package com.rew.canteenbox.bases;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.loopj.android.http.TextHttpResponseHandler;
import com.rew.canteenbox.http.HttpClient;
import cz.msebera.android.httpclient.Header;

/**
 *  基础类.
 *  将类默认引入总控台控制.
 *
 * Created by HuiWen Ren on 2016/10/15.
 */
public class BaseActivity extends AppCompatActivity implements IInit {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Log.d("BaseActivity", getClass().getSimpleName());
        // 添加进任务列表.
        ActivityController.addActivity(this);
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        // 从任务列表中除去.
        ActivityController.removeActivity(this);
    }

    @Override
    public int getLayoutId() {
        return 0;
    }

    @Override
    public void init() {

    }

    @Override
    public void onDataOK(String url, String data) {

    }

    @Override
    public void onNetworkUnavailable(String url) {

    }

    /**
     * GET请求
     *
     * @param url
     */
    public void get(final String url) {
//        if (!NetworkUtil.getInstance().checkNetworkAvailable()) {
//            onNetworkUnavailable(url);
//            return;
//        }
        HttpClient.get(url, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                String error = throwable.toString();
//                String response = responseString;
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                if (statusCode == 200) {
                    onDataOK(url, responseString);
                }
            }
        });
    }
}
